package com.fzh.controller.front;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fzh.pojo.dto.ProductLinkImageDTO;
import com.fzh.pojo.entity.Address;
import com.fzh.pojo.entity.Cart;
import com.fzh.pojo.entity.City;
import com.fzh.pojo.entity.Comment;
import com.fzh.pojo.entity.Images;
import com.fzh.pojo.entity.Message;
import com.fzh.pojo.entity.OrderDetails;
import com.fzh.pojo.entity.OrderInfo;
import com.fzh.pojo.entity.Product;
import com.fzh.pojo.entity.ProductType;
import com.fzh.pojo.entity.User;
import com.fzh.service.AddressService;
import com.fzh.service.CartService;
import com.fzh.service.CityService;
import com.fzh.service.CommentService;
import com.fzh.service.ImagesService;
import com.fzh.service.MessageService;
import com.fzh.service.OrderDetailsService;
import com.fzh.service.OrderInfoService;
import com.fzh.service.ProductService;
import com.fzh.service.ProductTypeService;
import com.fzh.service.UserService;

@Controller
public class IndexController {
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private ProductTypeService productTypeService;
	
	@Autowired
	private ImagesService imagesService;
	
	@Autowired
	private CartService cartService;
	
	@Autowired
	private AddressService addressService;
	
	@Autowired
	private OrderInfoService orderInfoService;
	
	@Autowired
	private OrderDetailsService orderDetailsService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private CityService cityService;
	
	@Autowired
	private CommentService commentService;
	
	@Autowired
	private MessageService messageService;
	/**
	 * 带着数据到前台首页，展示数据
	 * @param model
	 * @return
	 */
	@RequestMapping({"/","/index","/frontPage/index"})
	public String toIndex(Model model,HttpSession session) {
		//查询轮播商品(查询没有下架,显示滚动图,图片权重降序limit4)
		List<Product> bannerPro = productService.selectBannerPro();
		//查询推荐商品(查询没有下架,不显示滚动图,产品权重降序limit3)
		List<Product> productTJ = productService.selectProductTJ();
		//查询全部商品(查询没有下架,权重降序)
		List<Product> productAll = productService.queryAllProduct();
		//查询全部分类(根据权重)
		List<ProductType> productTypes = productTypeService.selectAllProductTypeByPid();
		//一级分类
		List<ProductType> productTypes1 = productTypeService.selectAllProductTypeByP_id(0);
		//查询所有分类
		List<ProductType> productTypes2 = productTypeService.queryAllProduct();
		
		model.addAttribute("bannerPro", bannerPro);
		model.addAttribute("productTJ", productTJ);
		model.addAttribute("productAll", productAll);
		session.setAttribute("productTypes1", productTypes1);
		session.setAttribute("productTypes2", productTypes2);
		session.setAttribute("productTypes", productTypes);
		return "frontPage/index";
	}
	
	/**
	 * 带着数据到前台首页，展示数据
	 * @param model
	 * @return
	 */
	@RequestMapping({"/frontPage/productDetail/{id}","/front/productDetail/{id}"})
	public String toProDetail(@PathVariable("id") int id,Model model) {
		//商品详情与图片(产品id)
		List<ProductLinkImageDTO> productDetail = productService.selectProductDetail(id);
		//商品详情(产品id)
		Product product = productService.selectProductDetailXX(id);
		//商品详情大图片(权重为5+产品id)
		List<Images> imageProduct = imagesService.queryImagesByWeight(5,id);
		//根据商品id查询留言信息
		List<Message> messageProduct = messageService.selectByProId(id);
		
		model.addAttribute("productDetail", productDetail);
		model.addAttribute("product", product);
		model.addAttribute("imageProduct", imageProduct);
		model.addAttribute("messageProduct", messageProduct);
		return "frontPage/proDetail";
	}
	
	@RequestMapping("/frontPage/productListMh")
	public String toProListAll(Product product,Model model) {
		List<Product> productTypeList = productService.selectProductTypeListMh(product);
		model.addAttribute("productTypeList", productTypeList);
		return "frontPage/proList";
	}
	
	@RequestMapping("/frontPage/productList/{id}")
	public String toProList(@PathVariable("id") int id,Model model) {
		List<Product> productTypeList = productService.selectProductTypeList(id);
		model.addAttribute("productTypeList", productTypeList);
		return "frontPage/proList";
	}
	
	/**
	 * 当带的商品类型id时跳转到商品列
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/toProductList/{id}")
	public String toProductList(@PathVariable("id") String id,Model model) {
		//商品所有数据
		List<Product> products = productService.queryAllProduct();
		//类型所有的数据
		List<ProductType> productTypes = productTypeService.queryAllProduct();
		//一级分类数据
		List<ProductType> oneEvlProductTypes = productTypeService.queryTwoEvlProductType();
		//图片的所有的数据
		List<Images> images = imagesService.queryAllImages();
		//查询最新添加的商品6条根据id降序排列，参数一从哪里开始，参数二显示条数
		List<Product> newProducts = productService.queryAllNewProsuctByIdDescF(0,6);
		model.addAttribute("productTypeId", Integer.parseInt(id));
		model.addAttribute("products", products);
		model.addAttribute("newProducts", newProducts);
		model.addAttribute("oneEvlProductTypes", oneEvlProductTypes);
		model.addAttribute("productTypes", productTypes);
		model.addAttribute("images", images);
		
		return "frontPage/product-list";
	}
	
	/**
	  *  当带的既有商品类型id又有商品类型的父id时跳转到商品列
	 * @param productTypeId
	 * @param model
	 * @return
	 */
	@GetMapping("/toProductListTypeInfo/{oneEvlProductTypeId}/{productTypeTwoId}")
	public String toProductListTypeInfo(
			@PathVariable("oneEvlProductTypeId") String productTypeId,
			@PathVariable("productTypeTwoId") String productTypeTwoId,
			Model model) {
		//商品所有数据
		List<Product> products = productService.queryAllProduct();
		//类型所有的数据
		List<ProductType> productTypes = productTypeService.queryAllProduct();
		//一级分类数据
		List<ProductType> oneEvlProductTypes = productTypeService.queryTwoEvlProductType();
		//图片的所有的数据
		List<Images> images = imagesService.queryAllImages();
		//查询最新添加的商品6条根据id降序排列，参数一从哪里开始，参数二显示条数
		List<Product> newProducts = productService.queryAllNewProsuctByIdDescF(0,6);
		model.addAttribute("productTypeId", Integer.parseInt(productTypeId));
		model.addAttribute("productTypeTwoId", Integer.parseInt(productTypeTwoId));
		model.addAttribute("products", products);
		model.addAttribute("newProducts", newProducts);
		model.addAttribute("oneEvlProductTypes", oneEvlProductTypes);
		model.addAttribute("productTypes", productTypes);
		model.addAttribute("images", images);
		return "frontPage/product-list";
	}
	
	/**
	  * 当带的既有商品类型id又有商品类型的父id又有商品id时跳转到商品的详情页面
	 * @param productTypeId
	 * @param model
	 * @return
	 */
	@GetMapping("/toProductItem/{oneEvlProductTypeId}/{productTypeTwoId}/{productId}")
	public String toProductListInfo(
			@PathVariable("oneEvlProductTypeId") String productTypeId,
			@PathVariable("productTypeTwoId") String productTypeTwoId,
			@PathVariable("productId") String productId,
			Model model) {
		//商品所有数据
		List<Product> products = productService.queryAllProduct();
		//类型所有的数据
		List<ProductType> productTypes = productTypeService.queryAllProduct();
		//一级分类数据
		List<ProductType> oneEvlProductTypes = productTypeService.queryTwoEvlProductType();
		//图片的所有的数据
		List<Images> images = imagesService.queryAllImages();
		//查询最新添加的商品6条根据id降序排列，参数一从哪里开始，参数二显示条数
		List<Product> newProducts = productService.queryAllNewProsuctByIdDescF(0,6);
		model.addAttribute("productTypeId", Integer.parseInt(productTypeId));
		model.addAttribute("productTypeTwoId", Integer.parseInt(productTypeTwoId));
		model.addAttribute("productId", Integer.parseInt(productId));
		model.addAttribute("products", products);
		model.addAttribute("newProducts", newProducts);
		model.addAttribute("oneEvlProductTypes", oneEvlProductTypes);
		model.addAttribute("productTypes", productTypes);
		model.addAttribute("images", images);
		return "frontPage/item";
	}
	
	/**
	 * 到登录页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/toLoginPage")
	public String toLoginPage(Model model) {
		//商品所有数据
		List<Product> products = productService.queryAllProduct();
		//类型所有的数据
		List<ProductType> productTypes = productTypeService.queryAllProduct();
		//一级分类数据
		List<ProductType> oneEvlProductTypes = productTypeService.queryTwoEvlProductType();
		//图片的所有的数据
		List<Images> images = imagesService.queryAllImages();
		//查询最新添加的商品6条根据id降序排列，参数一从哪里开始，参数二显示条数
		List<Product> newProducts = productService.queryAllNewProsuctByIdDescF(0,6);
		model.addAttribute("products", products);
		model.addAttribute("oneEvlProductTypes", oneEvlProductTypes);
		model.addAttribute("productTypes", productTypes);
		model.addAttribute("newProducts", newProducts);
		model.addAttribute("images", images);
		return "frontPage/login-page";
	}
	
	/**
	  * 当点击购物车内容执行删除对应商品
	 * @return
	 */
	@RequestMapping("/delCartProductByUserIdAndProductIdF/{productId}/{userId}")
	@ResponseBody
	public Boolean delCartProductByUserIdAndProductIdF(@PathVariable("productId")Integer productId,@PathVariable("userId")Integer userId,HttpSession session) {
		Boolean resMsg =  cartService.delCartProductByUserIdAndProductIdF(productId,userId);
		//查询用户信息与购物车信息
		if(session.getAttribute("user") != null && session.getAttribute("user") != "") {
			User user = (User) session.getAttribute("user");
			List<Cart> carts = cartService.queryCartByUserIdF(user.getId());
			session.setAttribute("carts", carts);
			String count1 = cartService.queryCartCountByUserIdF(user.getId());
			String totalSum = cartService.queryCartTotalSumByUserIdF(user.getId());
			session.setAttribute("count", count1);
			session.setAttribute("totalSum", totalSum);
		}
		return resMsg;
	}
	
	/**
	 * {前台}从商品详情页面添加一个购物车商品
	 * @param userId
	 * @param productId
	 * @param count
	 * @param cart
	 * @return
	 */
	@RequestMapping("/addCartByUserIdAndProductIdF/{userId}/{productId}/{count}")
	@ResponseBody
	public Boolean addCartByUserIdAndProductIdF(
			@PathVariable("userId")Integer userId,
			@PathVariable("productId")Integer productId,
			@PathVariable("count")Integer count,
			HttpSession session
			) {
		
		if(count == 0) {
			return false;
		}
		
		Cart carts1 = cartService.queryCartByUserIdAndProductIdF(userId,productId);
		Boolean resMsg = false;
		if(carts1 != null) {//如有该商品了就让购物车商品数量加一
			carts1.setCount(carts1.getCount()+count);
			carts1.setTotalSum(carts1.getPrice()*carts1.getCount());
			resMsg = cartService.editCartById(carts1);
		}else {//如没有该商品让购物车添加新的商品
			Product product = productService.queryProductByIdWitchCartInput(productId);
			Cart cart = new Cart(userId,productId,product.getName(),
					product.getImgMain(),product.getNewPrice(),product.getSize(),count,product.getNewPrice()*count);
			resMsg = cartService.addCartByUserIdAndProductIdF(cart);
		}
		//查询用户信息与购物车信息
		if(session.getAttribute("user") != null && session.getAttribute("user") != "") {
			User user = (User) session.getAttribute("user");
			List<Cart> carts = cartService.queryCartByUserIdF(user.getId());
			session.setAttribute("carts", carts);
			String count1 = cartService.queryCartCountByUserIdF(user.getId());
			String totalSum = cartService.queryCartTotalSumByUserIdF(user.getId());
			session.setAttribute("count", count1);
			session.setAttribute("totalSum", totalSum);
		}
		
		//查询全部分类(根据权重)
		List<ProductType> productTypes = productTypeService.selectAllProductTypeByPid();
		session.setAttribute("productTypes", productTypes);
		return resMsg;
	}	
	/**
	 * @param userId
	 * @param productId
	 * @param count
	 * @param session
	 * @return
	 */
	@RequestMapping("/editCartCountByCartId/{cartId}/{count}/{cartPrice}")
	@ResponseBody
	public Boolean editCartCountByUserIdAndProductIdF(
			@PathVariable("cartId") Integer cartId,
			@PathVariable("count") Integer count,
			@PathVariable("cartPrice") Double cartPrice,
			HttpSession session) {
			Cart cart = new Cart();
			cart.setId(cartId);
			cart.setCount(count);
			cart.setTotalSum(cartPrice*count);
			Boolean msg = cartService.editCartById(cart);
			return msg;
	}
	/**
	  *  到购物车页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/toShoppingCartPage/{userId}")
	public String toShoppingCartPage(@PathVariable("userId") Integer userId, Model model,HttpSession session) {
		//通过用户id查询用户购物车数据
		List<Cart> cartsByUserId = cartService.queryCartByUserIdF(userId);		
		model.addAttribute("cartsByUserId", cartsByUserId);
		model.addAttribute("userId", userId);
		
		//查询全部分类(根据权重)
		List<ProductType> productTypes = productTypeService.selectAllProductTypeByPid();
		session.setAttribute("productTypes", productTypes);
		return "frontPage/cart";
	}
	

	/**
	  *  删除购物车数据
	 * @param model
	 * @return
	 */
	@RequestMapping("/delCartById/{cartId}")
	@ResponseBody
	public String delCartById(@PathVariable("cartId") Integer cartId, Model model){
		Boolean flag = cartService.delCartById(cartId);
		String resu = null; 
		if(flag){
			resu = "seccess";
		} else {
			resu = "error";
		}
		return resu;
	}
	
	/**
	 * 去结算页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/toCheckoutPage/{userId}/{cartIds}")
	public String toCheckoutPage(@PathVariable("userId")Integer userId,@PathVariable("cartIds")String cartIds,Model model) {
		//查询出当前用户的所有购物车信息
		List<Cart> cartsByUserId = cartService.selectCartByIds(cartIds.split(","));	
		//查询出当前用户的地址信息
		List<Address> address = addressService.queryAllAddressByUserIdF(userId);
		//计算总的金额
		double totalPrice = 0;		
		for(int i=0;i<cartsByUserId.size();i++){
			totalPrice += cartsByUserId.get(i).getTotalSum();			
		}
		model.addAttribute("address", address);
		model.addAttribute("cartsByUserId", cartsByUserId);	
		model.addAttribute("totalPrice", totalPrice);
		model.addAttribute("cartIds", cartIds);
		return "frontPage/order";
	}
	
	/**
	 * 去个人中心页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/toMySelfPage/{userId}")
	public String toMySelfPage(@PathVariable("userId")String userId,Model model,HttpSession session){		
		//查询待支付订单数量
		int number0 = orderInfoService.queryOrderInfoCounts(userId, "", "0");
				
		//查询待发货订单数量
		int number1 = orderInfoService.queryOrderInfoCounts(userId, "", "1");
		//查询待收货订单数量
		int number2 = orderInfoService.queryOrderInfoCounts(userId, "", "2");
		//查询待评价订单数量
		int number3 = orderInfoService.queryOrderInfoCounts(userId, "", "3");
		//查询出当前用户的地址信息
		//List<Address> address = addressService.queryAllAddressByUserIdF(userId);
		model.addAttribute("userId", userId);
		model.addAttribute("number0", number0);
		model.addAttribute("number1", number1);
		model.addAttribute("number2", number2);
		model.addAttribute("number3", number3);
		
		//查询全部分类(根据权重)
		List<ProductType> productTypes = productTypeService.selectAllProductTypeByPid();
		session.setAttribute("productTypes", productTypes);
		return "frontPage/mygxin";
	}
	
	/**
	 * 查看订单信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/findOrderInfo/{userId}/{status}")
	public String findOrderInfo(@PathVariable("userId")String userId,@PathVariable("status")String status,Model model){
		List<OrderInfo> ordersInfo = null;
		if(status.equals("5")){
			ordersInfo = orderInfoService.queryAllOrderInfo(0, 100, userId, "", "");
		}else {
			ordersInfo = orderInfoService.queryAllOrderInfo(0, 100, userId, "", status);
		}
		
		model.addAttribute("ordersInfo", ordersInfo);
		model.addAttribute("userId", userId);
		return "frontPage/myorderq";
	}
	
	/**
	 * 查看订单详情信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/findOrderInfoDetails/{id}/{orderCode}/{userId}")
	public String findOrderInfoDetails(@PathVariable("id")Integer id,@PathVariable("orderCode")String orderCode,@PathVariable("userId")String userId,Model model){
		//查看订单详情信息
		OrderInfo orderInfo = orderInfoService.findOrderInfoDetails(id);
		//通过订单id查询管理的详情表数据
		List<OrderDetails> orders = orderDetailsService.selectByOrdId(orderCode);
		model.addAttribute("orderInfo", orderInfo);
		model.addAttribute("orders", orders);
		model.addAttribute("userId", userId);//回传用户id
		return "frontPage/orderxq";
	}
	
	/**
	 * 确定收货时改变状态
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/editOrderInfoByOrderStatus/{userId}/{orderCode}")
	public String editOrderInfoByOrderStatus(@PathVariable("userId")String userId,@PathVariable("orderCode")String orderId,Model model){
		orderInfoService.editOrderInfoByOrderStatus(orderId);
		model.addAttribute("userId", userId);//回传用户id
		return "frontPage/mygxin";
	}
	
	/**
	 * 催单时改变状态为确认收货
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/editOrderInfoByOrderStatus1/{userId}/{orderCode}")
	public String editOrderInfoByOrderStatus1(@PathVariable("userId")String userId,@PathVariable("orderCode")String orderId,Model model){
		orderInfoService.editOrderInfoByOrderStatus1(orderId);
		model.addAttribute("userId", userId);//回传用户id
		return "frontPage/mygxin";
	}
	
	/**
	 * 去地址管理页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/toAddressPage/{userId}")
	public String toAddressPage(@PathVariable("userId")Integer userId,Model model){
		//查询出当前用户的地址信息
		List<Address> address = addressService.queryAllAddressByUserIdF(userId);
		model.addAttribute("address", address);
		return "frontPage/address";
	}
	
	//删除地址
	@RequestMapping("/delAddressById/{id}")
	@ResponseBody
	public String delAddressById(@PathVariable("id")Integer id,Model model){
		addressService.deleteById(id);
		return "success";
	}
	
	@RequestMapping("/selectAddress/{id}")
	@ResponseBody
	public Object selectAddress(@PathVariable("id")Integer id){
		return addressService.selectAddressById(id);
	}
	
	@RequestMapping("/editAddress")
	@ResponseBody
	public Object editAddress(Address address,Model model){
		addressService.updateAddressById(address);
		return "success";
	}
	
	@RequestMapping("/addAddress")
	@ResponseBody
	public String addAddress(Address address,Model model){
		City city = cityService.selectCity(Integer.parseInt(address.getProvince()));
		address.setProvince(city.getName());
		City city1 = cityService.selectCity(Integer.parseInt(address.getCity()));
		address.setCity(city1.getName());
		City city2 = cityService.selectCity(Integer.parseInt(address.getArea()));
		address.setArea(city2.getName());
		addressService.addAddressById(address);
		return "success";
	}
	
	/**
	 * 去个人信息页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/tomymessagePage/{userId}")
	public String tomymessagePage(@PathVariable("userId") Integer userId, Model model) {		
		model.addAttribute("userId", userId);
		User user = userService.selectByuserId(userId);
		model.addAttribute("user",user);			
		return "frontPage/mygrxx";		
	}
	
	/**
	 * 编辑个人信息
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping("/toeditmymessagePage/{userId}")
	public String editmymessagePage(@PathVariable("userId") Integer userId, Model model,User user) {
				
		if (user != null) {
			model.addAttribute("user",user);	
			userService.updateUserById(user);
			model.addAttribute("remi", "修改成功!");
			return "frontPage/mygrxx";
		} else {
			 model.addAttribute("remi", "修改失败!");			
        }     
		model.addAttribute("userId", userId);
		return "frontPage/mygrxx";
	}
	
	/**
	 * 去修改密码页面
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping("/changepassword/{userId}")
	public String toLogin(@PathVariable("userId") Integer userId,Model model) {
		model.addAttribute("userId", userId);
		return "frontPage/remima";
	}	
	
	/**
	 * 修改密码
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/tochangepassword/{userId}")
	public String changepassword(@PathVariable("userId") Integer userId,HttpServletRequest request,Model model) {
		// 获取原密码
        String password = request.getParameter("password");
		// 获取新密码
        String newPassword = request.getParameter("newPassword");
        User userList = userService.selectLogins(userId,password);
        if (userList != null) {
			model.addAttribute("userId", userId);
			int result = userService.updatePassWord(userId,newPassword);
			model.addAttribute("remi", "修改成功!");
			return "frontPage/login";
		} else {
        	request.setAttribute("error", "原密码错误!");
        }
        model.addAttribute("remi", "修改失败!");
		return "frontPage/remima";		
	}
	
	/**
	 * 去支付页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/payment/{addressId}/{cartIds}")
	public String payment(@PathVariable("addressId")Integer addressId,@PathVariable("cartIds")String cartIds,Model model){
		//根据地址id查询数据
		Address asddress = addressService.selectAddressById(addressId);
		//根据cartIds查询多个商品信息
		List<Cart> cartList = cartService.selectCartByIds(cartIds.split(","));
		//创建订单数据
		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setProvince(asddress.getProvince());
		orderInfo.setCity(asddress.getCity());
		orderInfo.setArea(asddress.getArea());
		orderInfo.setAddressInfo(asddress.getAddressInfo());
		orderInfo.setName(asddress.getName());
		orderInfo.setPhone(asddress.getPhone());
		orderInfo.setStatus(1);    //待付款状态（0代表待付款，1代表待发货，2代表待收货，3代表待评价，4代表完成）
		//计算总的金额
		double totalPrice = 0;	
		String pNames = "";
		int pCount = 0;
		for(int i=0;i<cartList.size();i++){
				totalPrice += cartList.get(i).getTotalSum();	
				pNames += cartList.get(i).getName()+",";
				pCount += cartList.get(i).getCount();
		}
		orderInfo.setMoney(totalPrice);
		orderInfo.setpName(pNames);
		orderInfo.setCount(pCount);
		orderInfo.setF_uid(asddress.getF_uid());
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String format = dateFormat.format(new Date());
		orderInfo.setCreateTime(format);
		//生成一个订单号储存到订单数据里添加到数据库
		String format1 = dateFormat.format(new Date());
		String orderId = "3"+format1+"9";
		orderInfo.setOrderId(Long.parseLong(orderId));
		orderInfoService.addOrderInfo(orderInfo);
		//创建订单详情数据
		for(int i=0;i<cartList.size();i++){
			OrderDetails orderDetails = new OrderDetails();
			orderDetails.setOdId(orderId);
			orderDetails.setPdtId(cartList.get(i).getF_pid());
			orderDetails.setOdsSize(cartList.get(i).getSize());
			orderDetails.setOdsNumber(cartList.get(i).getCount());
			orderDetails.setProName(cartList.get(i).getName());
			orderDetails.setExt1(cartList.get(i).getImg());
			orderDetails.setExt2(cartList.get(i).getPrice()+"");
			orderDetails.setProPrice(cartList.get(i).getTotalSum()+"");
			//插入订单详情数据
			orderDetailsService.insertOrderDetails(orderDetails);
	    }	
		//删除被结算购物车的数据；
		cartService.delAllCartByIds(cartIds);
		int userId= asddress.getF_uid();
		//重定向到支付宝页面
		return "redirect:/toAlipayPage/"+orderId+"/"+totalPrice+"/"+userId;
	}	
	/**
	 * {前台}根据id查询出商品信息和商品对应的图片展示在前台视图弹出框内
	 * @param id
	 * @return
	 */
	@RequestMapping("/queryProductAndImagesByProductId/{productId}")
	@ResponseBody
	public Map<String,Object> queryProductById(@PathVariable("productId") Integer id) {
		Map<String,Object> map = new HashMap<String, Object>();
		Images images = imagesService.queryImagesByProductId(id);
		Product product = productService.queryProductByIdWitchCartInput(id);
		map.put("product", product);
		map.put("images", images);
		return map;
	}
}